import { 
  SET_PHONE_NUM,
  SET_VALIDATE_CODE,
  SET_COMPANY,
  SET_NAME,
  GET_VALIDATE_CODE,
  SET_VALIDATE_CODE_COUNT_DOWN,
  SET_VALIDATE_BUTTON_ENABLE,
  REGISTER,
  REGISTER_SUCCESS,
  REGISTER_FAIL,
} from '@/actions/register'

const initState = {
  phoneNum: '',
  validateCode: '',
  company: '',
  name: '',
  phoneVadSts: '',
  validateCodeEnable: true,
  validateCodeCountDown: '',
  registerButtonLoading: false,
}

const registerReducer = (state = initState, action) => {
  switch (action.type) {
    case SET_PHONE_NUM:
      let phoneVadSts;
      if(action.phoneNum.length === 11) phoneVadSts = 'success'
      else phoneVadSts = 'error'
      return { ...state, phoneNum: action.phoneNum, phoneVadSts }
      break;

    case SET_VALIDATE_CODE:
      return { ...state, validateCode: action.validateCode }
      break;

    case SET_COMPANY:
      return { ...state, company: action.company }
      break;

    case GET_VALIDATE_CODE:
      return { ...state, validateCodeEnable: false }
      break;

    case SET_NAME:
      return { ...state, name: action.name }
      break;

    case SET_VALIDATE_CODE_COUNT_DOWN:
      return { ...state, validateCodeCountDown: action.validateCodeCountDown }
      break;

    case SET_VALIDATE_BUTTON_ENABLE:
      return { ...state, validateCodeEnable: true }
      break;

    case REGISTER:
      return { ...state, registerButtonLoading: true }
      break;

    case REGISTER_SUCCESS:
      return { ...state, phoneNum: '',  validateCode: '',  company: '',  name: '', registerButtonLoading: false }
      break;

    case REGISTER_FAIL:
      return { ...state, registerButtonLoading: false }
      break;

    default:
      return state
      break;
  }
}

export default registerReducer